<template>
  <div class="callback"/>
</template>

<script>

import {loginByCode} from "@/api/login";
import {setToken} from "@/utils/auth";

export default {
  name: "callback",
  data() {
    return {
      code: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.code = route.query && route.query.code;
        ;
      },
      immediate: true
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      const code = this.code;
      const data = {
        "code": code
      }
      loginByCode(data).then(resp => {
        if (resp.code !== 0) {
          this.$router.push({path: "/login"}).catch()
        }
        setToken(resp.data.token);
        this.$store.commit('SET_TOKEN', resp.data.token);

        console.log(this.$router.history);
        this.$router.push({path: "/"}).catch();
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">

</style>
